package com.bichengcao.googlecodejam.qualification2010;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class SnapperChain {

	static int N;

	public static void main(String[] args) throws IOException {
		Input[] inputs = parseInput("src/com/bichengcao/googlecodejam/qualification2010/A-large.in");
		Output[] outputs = process(inputs);
		writeOutput("src/com/bichengcao/googlecodejam/qualification2010/SnapperChain.out", outputs);
	}

	static class Input {
		int n;
		int k;
	}

	static class Output {
		boolean on;
	}
	
	private static Input[] parseInput(String filename) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(filename));
		N = Integer.parseInt(in.readLine());
		Input[] array = new Input[N];

		for (int i = 0; i < N; i++) {
			Input input = array[i] = new Input();
			String line = in.readLine();
			StringTokenizer st = new StringTokenizer(line);
			input.n = Integer.parseInt(st.nextToken());
			input.k = Integer.parseInt(st.nextToken());
		}

		in.close();
		return array;
	}

	private static Output[] process(Input[] inputs) {
		Output[] outputs = new Output[N];
		for (int i = 0; i < N; i++) {
			outputs[i] = process(inputs[i]);
		}
		return outputs;
	}

	private static Output process(Input input) {
		Output output = new Output();

		output.on = ((input.k + 1) % (1 << input.n) == 0);

		return output;
	}

	private static void writeOutput(String filename, Output[] outputs) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
		for (int i = 0; i < N; i++) {
			Output output = outputs[i];
			out.print("Case #" + (i + 1) + ": ");
			if (output.on) {
				out.println("ON");
			} else {
				out.println("OFF");
			}
		}
		out.close();
	}

}
